/**
 * Created by Administrator on 2016/3/16.
 */
//返回功能
$(document).ready(function($){

    //分页JS
    $(".page_box li a").on('click', function(){
        var _that = this;
        if( typeof( $(_that).attr("action-data")) != "undefined" ){
            var page = parseInt($(_that).attr('action-data'));
            var uri = window.location.href.replace(/&page=([0-9]*)/g, '');
            window.location.href = uri+"&page="+page;
        }

    });
});

function toTop(){
    $('html, body').animate({scrollTop: 0},700);
}

function $ajax(uri, paramObj, func) {
    $.getJSON(uri, paramObj, function(data){
        if (func) func(data, function (data) {
            //显示状态
        });
    });

}

//区域获取关联
function ajaxGetRegion(paramObj,domID,func){
    $.getJSON("./api.php?m=Api&c=index&a=getRegion",paramObj, function(data){
        if(data.status!=0){
            alert("数据获取失败，请稍后重试或联系管理员解决！");
        }else{
            var $obj=$('#'+domID).empty();
            //$obj.append("<option value=\"0\">未定</option>");
            if(data.data){
                $.each(data.data,function(i,item){
                    $obj.append("<option value=\""+i+"\">"+item+"</option>");
                });
            }
            if(func){func();}
        }
    });
}

//点赞
function praiseArticle(obj,articleID){
    $.get("./index.php?m=Web&c=Article&a=praiseArticle&id="+articleID,function(result){
        if(result.status==0){
            $(obj).toggleClass('alreadyLikeBtn').toggleClass('likeBtn')
            if(typeof result.tip!='undefined'){
                layer.msg(result.tip,{shift:5,offset:$(window).height() * 0.8});
            }

        }else if(result.status==99){
            window.location.href = './index.php?m=Web&c=User&a=login';
        }
    },'json');
}

//喜欢
function likeArticle(obj,articleID){
    $.get("./index.php?m=Web&c=Article&a=praiseArticle&id="+articleID,function(result){
       if(result.status == 0){
           $(obj).toggleClass('noclick_like click_like').toggleClass('noclick_like');
           if($(obj).hasClass('click_like')){
               $(obj).html(__parseInt($(obj).html())+1);
           }else{
               $(obj).html(__parseInt($(obj).html())-1);
           }
       } else if(result.status==99){
           window.location.href = './index.php?m=Web&c=User&a=login';
       }
    },'json');
}

//收藏
function collectArticle(obj,aid){
    $.get("./index.php?m=Web&c=Article&a=collectArticle&id="+aid,function(result){
        if(result.status==0){
            //window.location.reload();
            $(obj).toggleClass('alreadyFavouriteBtn').toggleClass('favouriteBtn');
        }else if(result.status==99){
            window.location.href = './index.php?m=Web&c=User&a=login';
        }
    },'json');
}

//加关注
function follow(obj,fid){
    $.get("./index.php?m=Wap&c=Follow&a=follow&id="+fid,function(result){
        if(result.status==0){
            //切换关注已关注
            var followStatus = $(obj).val();
            var followNum = parseInt($('#num_'+fid).text());

            if(followStatus=="+ 关注"){
               $(obj).val("已关注");
                $('#num_'+fid).text(followNum+1);
            }else{
                $(obj).val("+ 关注");
                $('#num_'+fid).text(followNum-1);
            }
            $(obj).toggleClass('knb_button sizS').toggleClass('knb_button themeBlue sizS');
        }
    });
}

//验证手机号码
function validatePhone(phone) {
    var reg = /^0?1[3|4|5|7|8|6|9][0-9]\d{8}$/;
    if (!reg.test(phone)) {
        // $("#applyError .list").html('请出入正确的手机号码');
        // $("#applyError").show();
        //alert("请出入正确的手机号码");
        return false;
    }
    return true;
}

//修正用户头像
function fixUserHead(){
    $(".uhead").one("error", function(){
        $(this).attr("src", "./Public/Wap/images/hytx.png");
    });
}
//回到顶部
function toTop(){
    $('html, body').animate({scrollTop: 0},700);
}

function __parseInt(value){
    if(!value || isNaN(value))
        return 0;
    return parseInt(value,10);
}



